<?php
/**
 * XGPHP 轻量级PHP框架
 * @link http://xgphp.xg3.cn
 * @version 2.0.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author 讯高科技 <xungaokeji@qq.com>
*/
namespace xg;
use \helper\File;

// 应用类
class App extends Container {
	public static $app;
	protected $bindings = [
		'config'		 => Config::class,
		'response'		 => Response::class,
		'cookie'		 => Cookie::class,
		'env'			 => Env::class,
		// 'event'			 => Event::class,
		'lang'			 => Lang::class,
		'log'			 => Log::class,
		'request'		 => Request::class,
		'route'			 => Route::class,
		'session'		 => Session::class,
		'view'			 => View::class,
	];
	
	public function __construct(array $baseEnv=[]) {
		parent::__construct();
		
		if(!defined('XG_DEBUG')) {
			define('XG_DEBUG',$baseEnv['debug']);
		}
		
		$this->get('env')->set($baseEnv);
		
		$path = File::dir($baseEnv['site'],XG_ROOT);
		File::alias('site',$path);
		
		$dirFinder = function($name) use($baseEnv) {
			return File::dir($baseEnv[$name.'_path'],File::path('xg:site',$name));
		};
		
		if(is_dir($dirFinder('app'))) {
			File::alias('app',$dirFinder('app'));
		}else{
			File::alias('apps',$dirFinder('apps'));
		}
		
		File::alias('config',$dirFinder('config'));
		// File::alias('addons',$dirFinder('addons'));
		File::alias('runtime',$dirFinder('runtime'));
		File::alias('data',$dirFinder('data'));
	}
	
	public static function debug() {
		return defined('XG_DEBUG') and XG_DEBUG;
	}
	
	public function runRouter() {
		if(is_file(File::path('xg:app','common.php'))) {
			require_once File::path('xg:app','common.php');
		}elseif(is_file(File::path('xg:apps','common.php'))){
			require_once File::path('xg:apps','common.php');
		}
		
		// 初始化配置
		$route = $this->get('route');
		$response = $route->dispatch();
		if(!$response) {
			$response = $this->get('response');
		}elseif(is_string($response)) {
			$response = Response::drive('html')->content($response);
		}elseif(is_array($response)) {
			$response = Response::drive('json')->content($response);
		}
		
		// 如果返回了响应类 则发送响应内容
		if($response instanceof Response) {
			if($response->getContent()) {
				$response->send();
			}
		}
	}
	
	public static function create($baseEnv=null) {
		if(is_string($baseEnv)) {
			$baseEnv = Env::loadFile($baseEnv);
		}
		if(!is_array($baseEnv)) {
			$baseEnv = [];
		}
		$app = new self($baseEnv);
		return $app;
	}
	
	public static function run($envfile=null) {
		if(!self::$app) {
			File::AliasGroup('xg');
			File::alias('php',XG_PHP);
			File::alias('xg',dirname(XG_PHP));
			File::alias('envs',dirname(XG_PHP).XG_DS.'envs');

			set_exception_handler([ErrorHandler::class, 'appException']);
			
			// 运行中断处理
			register_shutdown_function([static::class, 'appShutdown']);

			self::$app = self::create($envfile);
			self::$app->runRouter();
		}
		return self::$app;
	}
	
    public static function getInstance() {
		if(!self::$app) {
			self::run();
		}
		return self::$app;
    }
	
	// 脚本终止
	public static function appShutdown() {
		self::$app->get('log')::saveAll();
	}
}
?>